package com.andatsoft.laisim.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.theme.ThemeManager;

public class SlimProgressBar extends View {

	public interface SlimProgressClicked {
		public void onClick(int newProgress);
	}

	// private Context mContext;
	private int mProgress = 0;// min 0; max: layout_width
	private int mMax = 100;

	// Attributes
	private int layout_width = 720;// the width of the view
	// private int layout_height = 10;// the height of the view
	private int mBackgroundColor;
	private int mProgressColor;
	private float mBackgroundWidth = 4f;// in dp
	private float mProgressWidth = 2f;// in dp
	private boolean mIsPressed;

	// Painters
	private Paint mBackgroundPainter;
	private Paint mBackgroundShadowPainter;
	private Paint mProgressPainter;
	private Paint mBackgroundPressedPainter;

	private SlimProgressClicked mISlimClicked;

	public int getBackgroundColor() {
		return mBackgroundColor;
	}

	public void setBackgroundColor(int mBackgroundColor) {
		this.mBackgroundColor = mBackgroundColor;
	}

	public int getProgressColor() {
		return mProgressColor;
	}

	public void setProgressColor(int mProgressColor) {
		this.mProgressColor = mProgressColor;
	}

	public float getBackgroundWidth() {
		return mBackgroundWidth;
	}

	public void setBackgroundWidth(float mBackgroundWidth) {
		this.mBackgroundWidth = mBackgroundWidth;
	}

	public float getProgressWidth() {
		return mProgressWidth;
	}

	public void setProgressWidth(float mProgressWidth) {
		this.mProgressWidth = mProgressWidth;
	}

	public SlimProgressBar(Context c) {
		super(c);
		// mContext = c;
		applySkin();
	}

	public SlimProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// mContext = context;
		applySkin();
		initAttributes(context.obtainStyledAttributes(attrs,
				R.styleable.ArcProgressBar));
	}

	private void initAttributes(TypedArray tA) {
		mBackgroundColor = tA.getColor(
				R.styleable.ArcProgressBar_backgroundColor, mBackgroundColor);
		mProgressColor = tA.getColor(R.styleable.ArcProgressBar_progressColor,
				mProgressColor);
		mBackgroundWidth = tA.getDimension(
				R.styleable.ArcProgressBar_backgroundWidth, mBackgroundWidth);
		mProgressWidth = tA.getDimension(
				R.styleable.ArcProgressBar_progressWidth, mProgressWidth);
		tA.recycle();
	}

	private void applySkin() {
		if (ThemeManager.getSelectedSkin() == null)
			return;
		mBackgroundColor = ThemeManager.getSelectedSkin()
				.getProgressBarHorizontalBackgroundColor();
		mProgressColor = ThemeManager.getSelectedSkin()
				.getProgressBarHorizontalProgressColor();
		mBackgroundWidth = ThemeManager.getSelectedSkin()
				.getProgressBarHorizontalBackgroundWidth();
		mProgressWidth = ThemeManager.getSelectedSkin()
				.getProgressBarHorizontalProgressWidth();
	}

	public void updateSkin() {
		applySkin();
		setupPainters();
		invalidate();
	}

	private void setupPainters() {
		if (mBackgroundPainter == null)
			mBackgroundPainter = new Paint();
		mBackgroundPainter.setColor(mBackgroundColor);
		mBackgroundPainter.setAntiAlias(true);
		mBackgroundPainter.setStyle(Style.STROKE);
		mBackgroundPainter.setStrokeWidth(mBackgroundWidth);
		mBackgroundPainter.setAlpha(50);

		if (mBackgroundShadowPainter == null)
			mBackgroundShadowPainter = new Paint();
		mBackgroundShadowPainter.setColor(mBackgroundColor);
		mBackgroundShadowPainter.setAntiAlias(true);
		mBackgroundShadowPainter.setStyle(Style.STROKE);
		mBackgroundShadowPainter.setStrokeWidth(mBackgroundWidth * 2);
		mBackgroundShadowPainter.setAlpha(30);

		if (mProgressPainter == null)
			mProgressPainter = new Paint();
		mProgressPainter.setColor(mProgressColor);
		mProgressPainter.setAntiAlias(true);
		mProgressPainter.setStyle(Style.STROKE);
		mProgressPainter.setStrokeWidth(mProgressWidth);

		if (mBackgroundPressedPainter == null)
			mBackgroundPressedPainter = new Paint();
		mBackgroundPressedPainter.setColor(mProgressColor);
		mBackgroundPressedPainter.setAntiAlias(true);
		mBackgroundPressedPainter.setStyle(Style.STROKE);
		mBackgroundPressedPainter.setStrokeWidth(mProgressWidth * 2);

	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		layout_width = w;
		// // limit the max height of this progress
		// if (h > 20)
		// h = 20;
		// layout_height = h;
		setupPainters();
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (mMax <= 0)
			mMax = 1;
		// draw background shadow
		// canvas.drawArc(mCircleBounds, 180, 180, false,
		// mBackgroundShadowPainter);
		// draw the background
		canvas.drawLine(0, 0, layout_width, 0, mBackgroundShadowPainter);
		canvas.drawLine(0, 0, layout_width, 0, mBackgroundPainter);
		canvas.drawLine(0, mBackgroundWidth / 2, mProgress * layout_width
				/ mMax, mBackgroundWidth / 2, mProgressPainter);
		if (mIsPressed) {
			canvas.drawLine(0, mBackgroundWidth / 2, mProgress * layout_width
					/ mMax, mBackgroundWidth / 2, mBackgroundPressedPainter);
		}
		// canvas.drawPath(mProgressPath, mProgressPainter);
		// draw the progress
		// canvas.drawArc(mCircleBounds, 180, mProgress, false,
		// mProgressPainter);

		// PointF mPoint1 = new PointF(this.getWidth() / 2 - mImage.getWidth() /
		// 2, this.getHeight() - mImage.getHeight());
		// PointF mPoint2 = new PointF(this.getWidth() / 2,this.getHeight());
		// Path myPath1 = new Path();
		// Paint paint = new Paint();
		// paint.setAntiAlias(true);
		// paint.setStyle(Style.STROKE);
		// paint.setStrokeWidth(2);
		// paint.setColor(Color.WHITE);
		//
		// myPath1 = drawCurve(canvas, paint, mPoint1, mPoint2);
		// canvas.drawPath(myPath1, paint);

	}

	// private Path drawCurve(Canvas canvas, Paint paint, PointF mPointa, PointF
	// mPointb) {
	//
	// Path myPath = new Path();
	// myPath.moveTo(63*1200/64, 768/10);
	// myPath.quadTo(mPointa.x, mPointa.y, mPointb.x, mPointb.y);
	// return myPath;
	// }
	//

	/**
	 * @param progress
	 *            : The progress of task. From 0 to 100
	 */
	public synchronized void setProgress(int progress) {
		if (progress < 0)
			progress = 0;
		if (progress > mMax)
			progress = mMax;
		this.mProgress = progress;
		invalidate();
	}

	public int getProgress() {
		return mProgress;
	}

	public int getMax() {
		return mMax;
	}

	public void setMax(int max) {
		this.mMax = max;
	}

	public void setOnSlimClicked(SlimProgressClicked click) {
		this.mISlimClicked = click;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int progress = this.mProgress;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			progress = (int) (event.getX() * mMax / layout_width);
			setProgress(progress);
			if (mISlimClicked != null)
				mISlimClicked.onClick(this.mProgress);
			mIsPressed = true;
			invalidate();
			return true;
		case MotionEvent.ACTION_MOVE:
			progress = (int) (event.getX() * mMax / layout_width);
			setProgress(progress);
			if (mISlimClicked != null)
				mISlimClicked.onClick(this.mProgress);
			mIsPressed = true;
			invalidate();
			return true;
		case MotionEvent.ACTION_UP:
			mIsPressed = false;
			invalidate();
			return true;
		}
		if (event.getAction() == MotionEvent.ACTION_DOWN) {

		}
		return true;
	}
}
